package com.icetec.iceintel.database;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import java.util.ArrayList;

import com.icetec.iceintel.model.Device;

/**
 * Created by amo on 2014/12/2.
 * 群组控制的关系数据库
 * 一个群组ID表示一个唯一的群组
 * 一个群组内可以有多个灯具纳入控制范围
 */

public class GroupControlSqlHelper {


    public final static String TABLE_GROUP_CONTROL="grouptable";
    public final static String TABLE_GROUP_CONTROL_GROUPID="id";
    public final static String TABLE_GROUP_CONTROL_MAC="mac";

    private ApplicationSQLiteHelper applicationSQLiteHelper=ApplicationSQLiteHelper.sharedInstance();


    public final static String CREATE_TABLE = "create table if not exists "+TABLE_GROUP_CONTROL+" ("
            + TABLE_GROUP_CONTROL_MAC
            + " varchar(20) , "
            + TABLE_GROUP_CONTROL_GROUPID
            + " varchar(10)  "
            +")";


    public  final static String GET_ALLDEVICES=" select "+TABLE_GROUP_CONTROL_MAC+" from "+TABLE_GROUP_CONTROL+" where "+TABLE_GROUP_CONTROL_GROUPID+"=?";

    public  final static String IS_DEVICE_EXIST=" select "+TABLE_GROUP_CONTROL_MAC+" from "+TABLE_GROUP_CONTROL+" where "+TABLE_GROUP_CONTROL_GROUPID+" =? and "+TABLE_GROUP_CONTROL_MAC+" =?";

    public final static String ADD_SINGLEDEVICES="insert into "+TABLE_GROUP_CONTROL+"("+TABLE_GROUP_CONTROL_MAC+","+TABLE_GROUP_CONTROL_GROUPID+") values(?,?)";

    public final static String UPDATE_GROUP_ID = "update " + TABLE_GROUP_CONTROL + " set " + TABLE_GROUP_CONTROL_GROUPID + "=?" + " where " + TABLE_GROUP_CONTROL_GROUPID + "=?";

    public void saveAllDeviceReArrange(ArrayList<Device> deviceArrayList,Device groupDevice){
        SQLiteDatabase db=applicationSQLiteHelper.getWritableDatabase();
        db.beginTransaction();
        for(Device device:deviceArrayList){
            saveSingle(device,groupDevice);
        }
        db.setTransactionSuccessful();
        db.endTransaction();
    }



    public void saveSingle(Device device,Device groupDevice){
        Log.i("SqlDevice", "储存单个群组devices,MAC=" + device.getMAC());
        SQLiteDatabase db=applicationSQLiteHelper.getWritableDatabase();
        Cursor cursor=  db.rawQuery(IS_DEVICE_EXIST,new String[]{device.getMAC(),groupDevice.getId().toString()});
        if(cursor.moveToNext()){



        }

        else {
            //不存在则插入信息
            db.execSQL(
                    ADD_SINGLEDEVICES,
                    new String[]{
                            device.getMAC(),
                            String.valueOf(groupDevice.getId())});

        }
    }


    public ArrayList<Device> getAllDevice(Device groupDevice){
     //   Log.i("数据库操作", "开始以ID检索全部设备");
        ArrayList<Device> array=new ArrayList<Device>();
        DevicesListSqlHelper devicesListSqlHelper = new DevicesListSqlHelper();
        SQLiteDatabase db = applicationSQLiteHelper.getReadableDatabase();
        db.beginTransaction();
        Cursor cursor = db.rawQuery(GET_ALLDEVICES, new String[]{String.valueOf(groupDevice.getId())});
        for (cursor.moveToFirst(); !cursor.isAfterLast();cursor.moveToNext()) {
            Device device = devicesListSqlHelper.getSingleDevice(cursor.getString(cursor.getColumnIndex(TABLE_GROUP_CONTROL_MAC)));
            array.add(device);
        }
        cursor.close();
        db.setTransactionSuccessful();
        db.endTransaction();
        return  array;
    }

    public void deledeSingleDevice(Device groupDevice){

        Log.i("数据库操作", "删除单个设备");
        SQLiteDatabase db = applicationSQLiteHelper.getReadableDatabase();
        db.delete(TABLE_GROUP_CONTROL, TABLE_GROUP_CONTROL_GROUPID + "=?", new String[]{String.valueOf(groupDevice.getId())});
    }


    public void updateGroupId(SQLiteDatabase db, Device groupDevice, int newId){
        db.execSQL(UPDATE_GROUP_ID, new String[]{String.valueOf(newId), String.valueOf(groupDevice.getId())});
    }


}
